/*
 * @lc app=leetcode.cn id=999 lang=javascript
 *
 * [999] 可以被一步捕获的棋子数
 */

// @lc code=start
/**
 * @param {character[][]} board
 * @return {number}
 */
var numRookCaptures = function (board) {
  let posX, posY;
  for (let i = 0; i < board.length; i++) {
    for (let j = 0; j < board[i].length; j++) {
      if (board[i][j] === 'R') {
        posX = i;
        posY = j;
        break;
      }
    }
    if (posX !== undefined) {
      break;
    }
  }
  let sum = 0;
  for (let i = posX - 1; i >= 0; i--) {
    if (board[i][posY] === 'p') {
      sum++;
      break;
    } else if (board[i][posY] === 'B') {
      break;
    }
  }
  for (let i = posX + 1; i < board.length; i++) {
    if (board[i][posY] === 'p') {
      sum++;
      break;
    } else if (board[i][posY] === 'B') {
      break;
    }
  }
  for (let j = posY - 1; j >= 0; j--) {
    if (board[posX][j] === 'p') {
      sum++;
      break;
    } else if (board[posX][j] === 'B') {
      break;
    }
  }
  for (let j = posY + 1; j < board[posX].length; j++) {
    if (board[posX][j] === 'p') {
      sum++;
      break;
    } else if (board[posX][j] === 'B') {
      break;
    }
  }
  return sum;
};
// @lc code=end
